<%@page contentType="text/html; charset=utf-8"%>
<%@ include file="../inc/TagLibs.jsp"%>
<c:set var="pageTitle" value="发消息" />
<%@ include file="../inc/Title.jsp"%>
<head>
<link rel="stylesheet" type="text/css" href="/css/base.css" />
<link rel="stylesheet" href="/kindeditor/themes/default/default.css" />
<script charset="utf-8" src="/kindeditor/kindeditor.js"></script>
<script charset="utf-8" src="/kindeditor/lang/zh_CN.js"></script>
<script src="/js/jquery.js"></script>
<script src="/js/EkUpload.js"></script>
<script language="javascript">
	function doSave(){
		document.getElementById("buttonDiv").style.display="none";
		document.getElementById("waitDiv").style.display="";
		editor.sync();
		if(editor.isEmpty()) {
			parent.ShowAlert('提示框','请输入内容!',200,100);
			document.getElementById("buttonDiv").style.display="";
			document.getElementById("waitDiv").style.display="none";
			return false;
		}else{
			editorForm.submit();
		}
	}
	function goBack(){
		window.history.back(-1);
	}	
		
	function selectReceiver(){
		var url="/base/StafferMainForSelect.ihtm?type=MULTI";
		parent.ShowIframe("选择员工",url,800,500,"selectReceiverCallBack()",1024,600);//selectReceiverCallBack是父窗口的
	}
	function selectReceiverAct(retValue){//父窗口的selectReceiverCallBack调用
		var idStr="";
		var nameStr="";
		for(i=0;i<retValue.length;i++){
			if(idStr!=""){
				idStr=idStr+";";
				nameStr=nameStr+";";
			}
			idStr=idStr+retValue[i][0];
			nameStr=nameStr+retValue[i][2];
		}
		document.getElementById("receiverIdStr").value=idStr;
		document.getElementById("receiverNameStr").value=nameStr;
		setFocus("message.title");
	}
	function showUplodPage(){
		if(window.applicationCache){//html5
			//每次都创建一个新的input file,否则连续选择相同文件时不能触发onchange
			if(document.getElementById("localFile")){
				document.getElementById("attTd").removeChild(document.getElementById("localFile"));
			}
			var localFile=document.createElement("input");
			localFile.setAttribute("type","file");
			localFile.setAttribute("multiple","multiple");
			localFile.setAttribute("id","localFile");
			localFile.style.display="none";
			localFile.onchange=doFileUpload;
			document.getElementById("attTd").appendChild(localFile);
			localFile.click();
		}else{
			parent.ShowIframeNoScroll("附件","/business/AccessoryNew.ihtm?code=${command.tmpCode}&agent_type=MESSAGE",560,320,"showAddedAcc();");
		}
	}
	function doFileUpload(){
		var files=document.getElementById("localFile").files;
		for(var i=0;i<files.length;i++){
			var lowerFn=files[i].name.toLowerCase();
			if(lowerFn.endWith(".exe") || lowerFn.endWith(".bat") || lowerFn.endWith(".com") || lowerFn.endWith(".jsp") || lowerFn.endWith(".htm") || lowerFn.endWith(".html")){
				parent.ShowAlert('提示框','不能上传.exe、.bat、.com、.jsp、.htm或.html格式的文件！',200,100);
				return;
			}
		}
		
		EkUpload.upload(
			files,
			function(msg){
				parent.ShowAlert('提示框',msg,200,100);
			},
			null,
			showUploadProc,
			doInsertRecord
		);
	}
	function showUploadProc(total,ci){
		document.getElementById("proTbl").style.display="";
		
		var totalStr=parseFileSize(total);
		var ciStr=parseFileSize(ci);
		
		var percentStr;
		if(total!=0){
			percentStr=(Math.round(ci*100/total)).toString()+"%";
			if(percentStr!="0%"){
				document.getElementById("proTd").style.width=percentStr;
				document.getElementById("percentTd").innerHTML=percentStr;
				if(ci==total){//100%
					document.getElementById("remainTd").style.display="none";
				}else{
					document.getElementById("remainTd").style.display="";
				}
			}
		}
		document.getElementById("proSpan").innerHTML="已传"+ciStr+"，总文件大小"+totalStr;
	}
	
	function doInsertRecord(fileName){
		var agentType="MESSAGE";
		var code="${command.tmpCode}";
		var url="/business/AccessoryInsertRecord.ihtm?return_type=AJAX&agent_type="+agentType+"&code="+code+"&file_name="+encodeURIComponent(fileName);
		var xmlHttp=createXMLHttpRequest();
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4) {
	      		if (xmlHttp.status == 200) {
					var itemId=xmlHttp.responseText;
	      			if(itemId!="0"){
						showAddedAccAct(itemId,fileName);
	      			}
				}
			}
		};
		xmlHttp.send(null);
	}
	function showAddedAccAct(id,fileName){//父窗口的showAddedAcc也调用这个
		var div=document.getElementById("attDiv");
		var li=document.createElement("li");
		li.setAttribute("id","att"+id);
		
		var tmpArr=fileName.split("/");
		li.appendChild(document.createTextNode(tmpArr[tmpArr.length-1]+"\u00A0\u00A0"));
		
		var delLink=document.createElement("a");
		delLink.setAttribute("href","javascript:doItemDelete("+id+");");
		delLink.appendChild(document.createTextNode("删除"));
		li.appendChild(delLink);
		
		div.appendChild(li);
		
		document.getElementById("linkSpan").style.display="";
		document.getElementById("noLinkSpan").style.display="none";
		document.getElementById("proTbl").style.display="none";
	}
	function doItemDelete(id){
		parent.ShowConfirm('确认框','确定删除吗？',340,80,"doDelAcc("+id+")");//doDelAcc是父窗口的
	}
	function doItemDeleteAct(id){//父窗口的doDelAcc调用
		var xmlHttp=createXMLHttpRequest();
		xmlHttp.open("GET", "/business/AccessoryDelete.ihtm?id="+id, true);
		xmlHttp.send(null);
		var li=document.getElementById("att"+id);
		var div=document.getElementById("attDiv");
		div.removeChild(li);
	}
	function keepAlive(){
		var currentTime=new Date();
		var xmlHttp=createXMLHttpRequest();
		xmlHttp.open("GET", "/Foot.ihtm?random="+currentTime.getTime(),true);
		xmlHttp.send(null);
		setTimeout("keepAlive()",1000*60*10);
	}
	function doInit(){
		showFormErrorMsg();
		document.getElementById("message.title").focus();
	}
	var editor;
	KindEditor.ready(function(K) {
    	editor = K.create('textarea[name="message.content"]', {
    			resizeType : 1,
				allowImageUpload : false,
				items : [
						'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
						'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
						'insertunorderedlist', 'indent', 'outdent','subscript', 'superscript', '|', 'emoticons', 'image', 'link', 'table', 'hr']
			});
    });
    function goEditor(){
    	if(window.event.keyCode==13){
    		editor.appendHtml('');
    	}
    }
</script>
</head>
<body oncontextmenu="return false" scroll="no" onLoad="doInit();keepAlive();" style="background: #EAFAFF;">
	<div id="scrollDiv" style="OVERFLOW:auto;height:468px;"">
		  <div class="main search-business">
			   <div class="cue-info">
					<c:set var="pageTopTitle" value="发消息" />
					<%@ include file="../inc/PageTop.jsp"%>
			   </div>
					<form name="editorForm" method="post">
						<table width="100%" border="0" cellpadding="1" cellspacing="1">
							<tr>
								<td align="center">
								<table border="0" cellspacing="1" cellpadding="1" align="center" class="table-0" style="width:98%">
									<tr>
								      	<td>
								      		<div align="left">收件人：
									      		<spring:bind path="command.receiverNameStr">
								      				<input type="text" class="TextBox readonly" style="width:300px" readOnly="true" id="${status.expression}" name="${status.expression}" value="${status.value}">
													<img class="make-a" src="/images/make-a.gif"   style="cursor:pointer;"  onClick="selectReceiver();"/>
												</spring:bind>
												<spring:bind path="command.receiverIdStr">
													<input type="hidden" id="${status.expression}" name="${status.expression}" value="${status.value}">
											          <script language="javascript">
											          	setFormErrorMsg('<c:out value="${status.errorMessage}" />',1);
											          </script>
												</spring:bind>
											</div>
								     	</td>
									</tr>
									<tr>
								      	<td>
								      		<div align="left">主&nbsp;&nbsp;&nbsp;题：
									      		<spring:bind path="command.message.title">
										      		<input type="text"  class="TextBox" style="width:300px" maxlength="50" onKeyUp="goEditor();" id="${status.expression}" name="${status.expression}" value="${status.value}">
											          <script language="javascript">
											          	setFormErrorMsg('<c:out value="${status.errorMessage}" />',2,'message.title');
											          </script>
									     		</spring:bind>
									     	</div>
										</td>								     
									</tr>
									<tr>
							      		<td align="left" id="attTd">
											<img src="/images/a32.gif" border="0">
											<span id="linkSpan"><a href="javascript:showUplodPage();">新增附件</a></span>
											<span id="noLinkSpan" style="display:none">新增附件</span>
											<input type="file" name="localFile" id="localFile" onchange="doFileUpload();" style="display:none" />
											
											<div id="attDiv">
												<c:forEach var="item" items="${command.accessories}"><li id="att${item.id}">${item.sourceName}&nbsp;&nbsp;<a href="javascript:doItemDelete(${item.id});">删除</a></li></c:forEach>
											</div>
											<table border="0" class="table-c" id="proTbl" style="display:none">
												<tr>
													<td style="width:40%">
														<table height="20" border="1" class="table-c">
															<tr>
																<td id="proTd" name="proTd" style="width:1px;" bgcolor="#3333cc"></td>
																<td id="remainTd" name="remainTd" bgcolor="#cccccc" style="width:100%">&nbsp;</td>
															</tr>
														</table>
													</td>
													<td id="percentTd" name="percentTd" style="width:5%">0%</td>
													<td id="proSpan" name="proSpan" style="width:55%">&nbsp;</td>
												</tr>
											</table>
										</td>
									</tr>
									<tr>
										<spring:bind path="command.message.content">
								      	<td>
								      		<textarea name="${status.expression}" id="${status.expression}" style="width:720px;height:240px;visibility:hidden;"><c:out value="${status.value}" /></textarea>
									        <script language="javascript">
									          	setFormErrorMsg('<c:out value="${status.errorMessage}" />',3);
									        </script>
								    	</td>
										</spring:bind>
									</tr>
								</table>
								</td>
							</tr>
						</table>		
						<div class="modal-buttom" id="buttonDiv">
							<input type="button" class="buttom-a" value=" 发 送 " onClick="doSave();">
							<input type="button" class="buttom-a" value=" 返 回 " onClick="goBack();">
							<spring:bind path="command.tmpCode"><input type="hidden" id="${status.expression}" name="${status.expression}" value="${status.value}"></spring:bind>
						</div>
						<div class="modal-buttom" id="waitDiv" style="display:none">
							<span>请稍候...</span>
						</div>
					</form>	
			</div>
		</div>
</body>
</html>